﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookManageSystem
{
    public partial class FormLogon: Form
    {
        public FormLogon()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogon_Click(object sender, EventArgs e)
        {
            //先判断文本框是否为空
            if (txtPswVerify.Text == "" || txtPsw.Text == "" || txtName.Text == ""
                || txtTel.Text == "" || cobSex.Text == ""||txtID.Text == "")
            {
                MessageBox.Show("有输入为空", "错误", MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            //注册 获取数据库中账号的最大值 然后+1
            Dao dao = new Dao();
            dao.Concnect();//连接并打开数据库
            string sql = "select MAX(Uid) from T_User";
            SqlDataReader reader = dao.read(sql);
            reader.Read();
            int id = 10001;
            if (reader[0] != null)
            {
                id = int.Parse(reader[0].ToString());
            }
            id++;
            string name = txtName.Text;
            string idCard = txtID.Text;
            string pwd = txtPsw.Text;
            string sex = cobSex.Text;
            string tel = txtTel.Text;

            //进行添加
            sql = $"insert into T_User values('{id}','{name}','{pwd}','{sex}','{idCard}','{tel}','1')";
            try {
                if (dao.Execute(sql) > 0)
                {
                    //注册成功
                    MessageBox.Show("注册成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message);
            }

            //else
            //{
            //    //注册失败
            //    MessageBox.Show("注册失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
            reader.Close();
            dao.DaoClose();
        }
    }
}
